﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Configuration;
using SE560Multimedia.src;

namespace SE560Multimedia.pages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SessionManager sessionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionManager = new SessionManager(Session);
        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            ProjectDataSetTableAdapters.mediaTableAdapter mediaTableAdapter =
                new ProjectDataSetTableAdapters.mediaTableAdapter();
            String mediaName = TextBoxMediaName.Text;
            String albumName = TextBoxAlbumName.Text;
            String artistName = TextBoxArtistName.Text;

            if (mediaName.Length == 0)
            {
                mediaName = "*";
            }
            if (albumName.Length == 0)
            {
                albumName = "*";
            }
            if (artistName.Length == 0)
            {
                artistName = "*";
            }

            ProjectDataSet.mediaDataTable searchResult =
                mediaTableAdapter.GetDataBySearchFields(
                mediaName, albumName, artistName,
                sessionManager.SessionLoggedUserId);

            sessionManager.PlaylistResult = searchResult;
            
            GridViewSearchResult.DataSource = searchResult;
            GridViewSearchResult.DataBind();

            String operationStatusString = null;
            if (searchResult.Rows.Count > 0)
            {
                operationStatusString = "Search Results are listing...";
            }
            else
            {
                operationStatusString = "No Results matching with given criteria...";
            }
            LabelSearchResult.Text = operationStatusString;
        }

        protected void GridViewSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ResultListPlayCommand")
            {
                int rowId = Convert.ToInt32( e.CommandArgument );
                sessionManager.CurrentDataRow = 
                    sessionManager.PlaylistResult[rowId];
                System.Web.HttpContext.Current.Response.Redirect(
                    SessionManager.PLAY_PAGE_ADDESS);
            }
        }
    }
}
